

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.ftl.data_util.common_data_util &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.ftl.data_util.common_data_util</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.ftl.data_util.common_data_util</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>
<span class="c1">#</span>

<span class="kn">import</span> <span class="nn">csv</span>
<span class="kn">import</span> <span class="nn">time</span>

<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">arch.api.eggroll</span> <span class="k">import</span> <span class="n">parallelize</span><span class="p">,</span> <span class="n">table</span>
<span class="kn">from</span> <span class="nn">federatedml.feature.instance</span> <span class="k">import</span> <span class="n">Instance</span>


<div class="viewcode-block" id="series_plot"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.series_plot">[docs]</a><span class="k">def</span> <span class="nf">series_plot</span><span class="p">(</span><span class="n">losses</span><span class="p">,</span> <span class="n">fscores</span><span class="p">,</span> <span class="n">aucs</span><span class="p">):</span>
    <span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">(</span><span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">40</span><span class="p">))</span>

    <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">311</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">losses</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;epoch&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;values&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">&quot;loss&quot;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

    <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">312</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">fscores</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;epoch&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;values&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">&quot;fscore&quot;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

    <span class="n">plt</span><span class="o">.</span><span class="n">subplot</span><span class="p">(</span><span class="mi">313</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">aucs</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;epoch&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;values&#39;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">&quot;auc&quot;</span><span class="p">)</span>
    <span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>

    <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span></div>


<div class="viewcode-block" id="balance_X_y"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.balance_X_y">[docs]</a><span class="k">def</span> <span class="nf">balance_X_y</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
    <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
    <span class="n">num_pos</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">y</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">num_neg</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">y</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">pos_indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">_y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="k">if</span> <span class="n">_y</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">]</span>
    <span class="n">neg_indexes</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">_y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="k">if</span> <span class="n">_y</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">num_pos</span> <span class="o">&lt;</span> <span class="n">num_neg</span><span class="p">:</span>
        <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">neg_indexes</span><span class="p">)</span>
        <span class="c1"># randomly pick negative samples of size equal to that of positive samples</span>
        <span class="n">rand_indexes</span> <span class="o">=</span> <span class="n">neg_indexes</span><span class="p">[:</span><span class="n">num_pos</span><span class="p">]</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="n">pos_indexes</span> <span class="o">+</span> <span class="n">rand_indexes</span>
        <span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indexes</span><span class="p">]</span>
        <span class="n">X</span> <span class="o">=</span> <span class="p">[</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indexes</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">X</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">y</span><span class="p">)</span></div>


<div class="viewcode-block" id="shuffle_X_y"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.shuffle_X_y">[docs]</a><span class="k">def</span> <span class="nf">shuffle_X_y</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">seed</span><span class="o">=</span><span class="mi">5</span><span class="p">):</span>
    <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span>
    <span class="n">data_size</span> <span class="o">=</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">shuffle_index</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">data_size</span><span class="p">))</span>
    <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">shuffle_index</span><span class="p">)</span>
    <span class="n">X</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">shuffle_index</span><span class="p">,</span> <span class="p">:]</span>
    <span class="n">y</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">shuffle_index</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span></div>


<div class="viewcode-block" id="load_data"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.load_data">[docs]</a><span class="k">def</span> <span class="nf">load_data</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">id_index</span><span class="p">,</span> <span class="n">feature_index_range</span><span class="p">,</span> <span class="n">label_index</span><span class="p">):</span>
    <span class="n">ids</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">X</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">y</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fi</span><span class="p">:</span>
        <span class="n">fi</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
        <span class="n">reader</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="n">fi</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">reader</span><span class="p">:</span>
            <span class="n">ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="n">id_index</span><span class="p">])</span>
            <span class="n">X</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="n">feature_index_range</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span> <span class="n">feature_index_range</span><span class="p">[</span><span class="mi">1</span><span class="p">]])</span>
            <span class="n">yo</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="n">label_index</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">yo</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">yo</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
            <span class="n">y</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">yo</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ids</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span></div>


<div class="viewcode-block" id="split_data_combined"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.split_data_combined">[docs]</a><span class="k">def</span> <span class="nf">split_data_combined</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">overlap_ratio</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">ab_split_ratio</span><span class="o">=</span><span class="mf">0.1</span><span class="p">,</span> <span class="n">n_feature_b</span><span class="o">=</span><span class="mi">16</span><span class="p">):</span>
    <span class="n">data_size</span> <span class="o">=</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">overlap_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">data_size</span> <span class="o">*</span> <span class="n">overlap_ratio</span><span class="p">)</span>
    <span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">overlap_size</span><span class="p">))</span>
    <span class="n">A_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">data_size</span> <span class="o">-</span> <span class="n">overlap_size</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">ab_split_ratio</span><span class="p">))</span>
    <span class="n">X_A</span> <span class="o">=</span> <span class="n">X</span><span class="p">[:</span><span class="n">A_size</span> <span class="o">+</span> <span class="n">overlap_size</span><span class="p">,</span> <span class="n">n_feature_b</span><span class="p">:]</span>
    <span class="n">y_A</span> <span class="o">=</span> <span class="n">y</span><span class="p">[:</span><span class="n">A_size</span> <span class="o">+</span> <span class="n">overlap_size</span><span class="p">,</span> <span class="p">:]</span>
    <span class="n">X_B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">X</span><span class="p">[:</span><span class="n">overlap_size</span><span class="p">,</span> <span class="p">:</span><span class="n">n_feature_b</span><span class="p">],</span> <span class="n">X</span><span class="p">[</span><span class="n">A_size</span> <span class="o">+</span> <span class="n">overlap_size</span><span class="p">:,</span> <span class="p">:</span><span class="n">n_feature_b</span><span class="p">]))</span>
    <span class="n">y_B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">y</span><span class="p">[:</span><span class="n">overlap_size</span><span class="p">,</span> <span class="p">:],</span> <span class="n">y</span><span class="p">[</span><span class="n">A_size</span> <span class="o">+</span> <span class="n">overlap_size</span><span class="p">:,</span> <span class="p">:]))</span>
    <span class="k">return</span> <span class="n">X_A</span><span class="p">,</span> <span class="n">y_A</span><span class="p">,</span> <span class="n">X_B</span><span class="p">,</span> <span class="n">y_B</span><span class="p">,</span> <span class="n">overlap_indexes</span></div>


<div class="viewcode-block" id="stack_overlap_nonoverlap"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.stack_overlap_nonoverlap">[docs]</a><span class="k">def</span> <span class="nf">stack_overlap_nonoverlap</span><span class="p">(</span><span class="n">data_dict</span><span class="p">,</span> <span class="n">overlap_data_indexes</span><span class="p">,</span> <span class="n">nonoverlap_data_indexes</span><span class="p">):</span>
    <span class="n">data_overlap</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">overlap_data_indexes</span><span class="p">:</span>
        <span class="n">data_overlap</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data_dict</span><span class="p">[</span><span class="n">index</span><span class="p">])</span>
    <span class="n">data_non_overlap</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="n">nonoverlap_data_indexes</span><span class="p">:</span>
        <span class="n">data_non_overlap</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data_dict</span><span class="p">[</span><span class="n">index</span><span class="p">])</span>
    <span class="n">data_overlap</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data_overlap</span><span class="p">)</span>
    <span class="n">data_non_overlap</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data_non_overlap</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">data_overlap</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">data_overlap</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data_non_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">data_non_overlap</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">data_non_overlap</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

    <span class="c1"># TODO data_non_overlap [] (0, 1)</span>
    <span class="n">data_stack</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">data_overlap</span><span class="p">,</span> <span class="n">data_non_overlap</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">data_stack</span></div>


<div class="viewcode-block" id="overlapping_samples_converter"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.overlapping_samples_converter">[docs]</a><span class="k">def</span> <span class="nf">overlapping_samples_converter</span><span class="p">(</span><span class="n">target_features_dict</span><span class="p">,</span> <span class="n">target_sample_indexes</span><span class="p">,</span> <span class="n">ref_sample_indexes</span><span class="p">,</span>
                                  <span class="n">target_labels_dict</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">intersect1d</span><span class="p">(</span><span class="n">target_sample_indexes</span><span class="p">,</span> <span class="n">ref_sample_indexes</span><span class="p">,</span> <span class="n">assume_unique</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">non_overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">setdiff1d</span><span class="p">(</span><span class="n">target_sample_indexes</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">)</span>

    <span class="n">new_overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">overlap_indexes</span><span class="p">)))</span>
    <span class="n">new_non_overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">overlap_indexes</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">overlap_indexes</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">non_overlap_indexes</span><span class="p">)))</span>

    <span class="n">features_stack</span> <span class="o">=</span> <span class="n">stack_overlap_nonoverlap</span><span class="p">(</span><span class="n">target_features_dict</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">,</span> <span class="n">non_overlap_indexes</span><span class="p">)</span>
    <span class="n">features</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">features_stack</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">target_labels_dict</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">features</span><span class="p">,</span> <span class="n">new_overlap_indexes</span><span class="p">,</span> <span class="n">new_non_overlap_indexes</span><span class="p">,</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">labels_stack</span> <span class="o">=</span> <span class="n">stack_overlap_nonoverlap</span><span class="p">(</span><span class="n">target_labels_dict</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">,</span> <span class="n">non_overlap_indexes</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">features</span><span class="p">,</span> <span class="n">new_overlap_indexes</span><span class="p">,</span> <span class="n">new_non_overlap_indexes</span><span class="p">,</span> <span class="n">labels_stack</span></div>


<div class="viewcode-block" id="split_into_guest_host_dtable"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.split_into_guest_host_dtable">[docs]</a><span class="k">def</span> <span class="nf">split_into_guest_host_dtable</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">overlap_ratio</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">guest_split_ratio</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">guest_feature_num</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span>
                                 <span class="n">tables_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="n">data_size</span> <span class="o">=</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">overlap_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">data_size</span> <span class="o">*</span> <span class="n">overlap_ratio</span><span class="p">)</span>
    <span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">overlap_size</span><span class="p">))</span>
    <span class="n">guest_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">data_size</span> <span class="o">-</span> <span class="n">overlap_size</span><span class="p">)</span> <span class="o">*</span> <span class="n">guest_split_ratio</span><span class="p">)</span>

    <span class="n">guest_table_ns</span> <span class="o">=</span> <span class="s2">&quot;guest_table_ns&quot;</span>
    <span class="n">guest_table_name</span> <span class="o">=</span> <span class="s2">&quot;guest_table_name&quot;</span>
    <span class="n">host_table_ns</span> <span class="o">=</span> <span class="s2">&quot;host_table_ns&quot;</span>
    <span class="n">host_table_name</span> <span class="o">=</span> <span class="s2">&quot;host_table_name&quot;</span>
    <span class="k">if</span> <span class="n">tables_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">guest_table_ns</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;guest_table_ns&quot;</span><span class="p">]</span>
        <span class="n">guest_table_name</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;guest_table_name&quot;</span><span class="p">]</span>
        <span class="n">host_table_ns</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;host_table_ns&quot;</span><span class="p">]</span>
        <span class="n">host_table_name</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;host_table_name&quot;</span><span class="p">]</span>

    <span class="n">guest_temp</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">overlap_size</span> <span class="o">+</span> <span class="n">guest_size</span><span class="p">):</span>
        <span class="n">guest_temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">Instance</span><span class="p">(</span><span class="n">inst_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">features</span><span class="o">=</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="n">guest_feature_num</span><span class="p">]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">])))</span>
    <span class="n">guest_data</span> <span class="o">=</span> <span class="n">table</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">guest_table_name</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="n">guest_table_ns</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="n">partition</span><span class="p">)</span>
    <span class="n">guest_data</span><span class="o">.</span><span class="n">put_all</span><span class="p">(</span><span class="n">guest_temp</span><span class="p">)</span>

    <span class="n">host_temp</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">overlap_size</span><span class="p">):</span>
        <span class="n">host_temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">Instance</span><span class="p">(</span><span class="n">inst_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">features</span><span class="o">=</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">guest_feature_num</span><span class="p">:]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">])))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">overlap_size</span> <span class="o">+</span> <span class="n">guest_size</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">X</span><span class="p">)):</span>
        <span class="n">host_temp</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
            <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">Instance</span><span class="p">(</span><span class="n">inst_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">features</span><span class="o">=</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">guest_feature_num</span><span class="p">:]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span> <span class="n">label</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">])))</span>
    <span class="n">host_data</span> <span class="o">=</span> <span class="n">table</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">host_table_name</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="n">host_table_ns</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="n">partition</span><span class="p">)</span>
    <span class="n">host_data</span><span class="o">.</span><span class="n">put_all</span><span class="p">(</span><span class="n">host_temp</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">guest_data</span><span class="p">,</span> <span class="n">host_data</span><span class="p">,</span> <span class="n">overlap_indexes</span></div>


<div class="viewcode-block" id="feed_into_dtable"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.feed_into_dtable">[docs]</a><span class="k">def</span> <span class="nf">feed_into_dtable</span><span class="p">(</span><span class="n">ids</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">sample_range</span><span class="p">,</span> <span class="n">feature_range</span><span class="p">,</span> <span class="n">tables_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create an eggroll table feed with data specified by parameters provided</span>

<span class="sd">    parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    :param ids: 1D numpy array</span>
<span class="sd">    :param X: 2D numpy array</span>
<span class="sd">    :param y: 2D numpy array</span>
<span class="sd">    :param sample_range: a tuple specifies the range of samples to feed into dtable</span>
<span class="sd">    :param feature_range: a tuple specifies the range of features to feed into dtable</span>
<span class="sd">    :param tables_name: a dictionary specifies table namespace (with key table_ns) and table name (with key table_name)</span>
<span class="sd">    :param partition: number of partition used when creating the dtable</span>
<span class="sd">    :return: an eggroll dtable</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">table_ns</span> <span class="o">=</span> <span class="s2">&quot;default_table_namespace&quot;</span>
    <span class="n">table_name</span> <span class="o">=</span> <span class="n">get_timestamp</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">tables_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">table_ns</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;table_ns&quot;</span><span class="p">]</span>
        <span class="n">table_name</span> <span class="o">=</span> <span class="n">tables_name</span><span class="p">[</span><span class="s2">&quot;table_name&quot;</span><span class="p">]</span>

    <span class="n">sample_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">sample_range</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">sample_range</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span>
        <span class="n">sample_list</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">ids</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">Instance</span><span class="p">(</span><span class="n">inst_id</span><span class="o">=</span><span class="n">ids</span><span class="p">[</span><span class="n">i</span><span class="p">],</span>
                                             <span class="n">features</span><span class="o">=</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">feature_range</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">feature_range</span><span class="p">[</span><span class="mi">1</span><span class="p">]],</span>
                                             <span class="n">label</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">])))</span>
    <span class="n">data_table</span> <span class="o">=</span> <span class="n">table</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">table_name</span><span class="p">,</span> <span class="n">namespace</span><span class="o">=</span><span class="n">table_ns</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="n">partition</span><span class="p">)</span>
    <span class="n">data_table</span><span class="o">.</span><span class="n">put_all</span><span class="p">(</span><span class="n">sample_list</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">data_table</span></div>


<div class="viewcode-block" id="create_data_generator"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.create_data_generator">[docs]</a><span class="k">def</span> <span class="nf">create_data_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">start_end_index_list</span><span class="p">,</span> <span class="n">feature_index_range</span><span class="p">):</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">start_index</span><span class="p">,</span> <span class="n">end_index</span><span class="p">)</span> <span class="ow">in</span> <span class="n">start_end_index_list</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">start_index</span><span class="p">,</span> <span class="n">end_index</span><span class="p">):</span>
            <span class="k">yield</span> <span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">Instance</span><span class="p">(</span><span class="n">inst_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">weight</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
                               <span class="n">features</span><span class="o">=</span><span class="n">X</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="n">feature_index_range</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span><span class="n">feature_index_range</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">),</span>
                               <span class="n">label</span><span class="o">=</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">0</span><span class="p">]))</span></div>


<div class="viewcode-block" id="create_guest_host_data_generator"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.create_guest_host_data_generator">[docs]</a><span class="k">def</span> <span class="nf">create_guest_host_data_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">overlap_ratio</span><span class="o">=</span><span class="mf">0.2</span><span class="p">,</span> <span class="n">guest_split_ratio</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">guest_feature_num</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="n">data_size</span> <span class="o">=</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">overlap_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">data_size</span> <span class="o">*</span> <span class="n">overlap_ratio</span><span class="p">)</span>
    <span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">overlap_size</span><span class="p">))</span>
    <span class="n">guest_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">data_size</span> <span class="o">-</span> <span class="n">overlap_size</span><span class="p">)</span> <span class="o">*</span> <span class="n">guest_split_ratio</span><span class="p">)</span>

    <span class="n">guest_data_generator</span> <span class="o">=</span> <span class="n">create_data_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="n">overlap_size</span> <span class="o">+</span> <span class="n">guest_size</span><span class="p">)],</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">guest_feature_num</span><span class="p">))</span>
    <span class="n">host_data_generator</span> <span class="o">=</span> <span class="n">create_data_generator</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="p">[(</span><span class="mi">0</span><span class="p">,</span> <span class="n">overlap_size</span><span class="p">),</span> <span class="p">(</span><span class="n">overlap_size</span> <span class="o">+</span> <span class="n">guest_size</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">X</span><span class="p">))],</span>
                                                <span class="p">(</span><span class="n">guest_feature_num</span><span class="p">,</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]))</span>

    <span class="k">return</span> <span class="n">guest_data_generator</span><span class="p">,</span> <span class="n">host_data_generator</span><span class="p">,</span> <span class="n">overlap_indexes</span></div>


<div class="viewcode-block" id="load_model_parameters"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.load_model_parameters">[docs]</a><span class="k">def</span> <span class="nf">load_model_parameters</span><span class="p">(</span><span class="n">model_table_name</span><span class="p">,</span> <span class="n">model_namespace</span><span class="p">):</span>
    <span class="n">model</span> <span class="o">=</span> <span class="n">table</span><span class="p">(</span><span class="n">model_table_name</span><span class="p">,</span> <span class="n">model_namespace</span><span class="p">)</span>
    <span class="n">model_parameters</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">meta_name</span><span class="p">,</span> <span class="n">meta_value</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">collect</span><span class="p">():</span>
        <span class="n">model_parameters</span><span class="p">[</span><span class="n">meta_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">meta_value</span>
    <span class="k">return</span> <span class="n">model_parameters</span></div>


<div class="viewcode-block" id="save_model_parameters"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.save_model_parameters">[docs]</a><span class="k">def</span> <span class="nf">save_model_parameters</span><span class="p">(</span><span class="n">model_parameters</span><span class="p">,</span> <span class="n">model_table_name</span><span class="p">,</span> <span class="n">model_namespace</span><span class="p">):</span>
    <span class="n">dtable</span> <span class="o">=</span> <span class="n">parallelize</span><span class="p">(</span><span class="n">model_parameters</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">include_key</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                         <span class="n">name</span><span class="o">=</span><span class="n">model_table_name</span><span class="p">,</span>
                         <span class="n">namespace</span><span class="o">=</span><span class="n">model_namespace</span><span class="p">,</span>
                         <span class="n">error_if_exist</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                         <span class="n">persistent</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">dtable</span></div>


<div class="viewcode-block" id="create_table"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.create_table">[docs]</a><span class="k">def</span> <span class="nf">create_table</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">indexes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model_table_name</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">model_namespace</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">persistent</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">indexes</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">dtable</span> <span class="o">=</span> <span class="n">parallelize</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">include_key</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
                             <span class="n">name</span><span class="o">=</span><span class="n">model_table_name</span><span class="p">,</span>
                             <span class="n">namespace</span><span class="o">=</span><span class="n">model_namespace</span><span class="p">,</span>
                             <span class="n">error_if_exist</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                             <span class="n">persistent</span><span class="o">=</span><span class="n">persistent</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">data_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">indexes</span><span class="p">):</span>
            <span class="n">data_dict</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
        <span class="n">dtable</span> <span class="o">=</span> <span class="n">parallelize</span><span class="p">(</span><span class="n">data_dict</span><span class="o">.</span><span class="n">items</span><span class="p">(),</span> <span class="n">include_key</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                             <span class="n">name</span><span class="o">=</span><span class="n">model_table_name</span><span class="p">,</span>
                             <span class="n">namespace</span><span class="o">=</span><span class="n">model_namespace</span><span class="p">,</span>
                             <span class="n">error_if_exist</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                             <span class="n">persistent</span><span class="o">=</span><span class="n">persistent</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">dtable</span></div>


<div class="viewcode-block" id="save_data_to_eggroll_table"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.save_data_to_eggroll_table">[docs]</a><span class="k">def</span> <span class="nf">save_data_to_eggroll_table</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">table_name</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="n">data_table</span> <span class="o">=</span> <span class="n">table</span><span class="p">(</span><span class="n">table_name</span><span class="p">,</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">partition</span><span class="o">=</span><span class="n">partition</span><span class="p">,</span> <span class="n">create_if_missing</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">error_if_exist</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">data_table</span><span class="o">.</span><span class="n">put_all</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">data_table</span></div>


<div class="viewcode-block" id="convert_dict_to_array"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.convert_dict_to_array">[docs]</a><span class="k">def</span> <span class="nf">convert_dict_to_array</span><span class="p">(</span><span class="n">data_dict</span><span class="p">):</span>
    <span class="n">data_list</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">data_dict</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="n">data_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data_list</span><span class="p">)</span></div>


<div class="viewcode-block" id="convert_instance_table_to_dict"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.convert_instance_table_to_dict">[docs]</a><span class="k">def</span> <span class="nf">convert_instance_table_to_dict</span><span class="p">(</span><span class="n">instances_table</span><span class="p">):</span>
    <span class="n">features_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">labels_dict</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">instances_indexes</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">instances_table</span><span class="o">.</span><span class="n">collect</span><span class="p">():</span>
        <span class="n">instances_indexes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
        <span class="n">features_dict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">features</span>
        <span class="n">labels_dict</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">label</span>
    <span class="k">return</span> <span class="n">features_dict</span><span class="p">,</span> <span class="n">labels_dict</span><span class="p">,</span> <span class="n">instances_indexes</span></div>


<div class="viewcode-block" id="convert_instance_table_to_array"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.convert_instance_table_to_array">[docs]</a><span class="k">def</span> <span class="nf">convert_instance_table_to_array</span><span class="p">(</span><span class="n">instances_table</span><span class="p">):</span>
    <span class="n">features</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">instances_indexes</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">instances_table</span><span class="o">.</span><span class="n">collect</span><span class="p">():</span>
        <span class="n">instances_indexes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
        <span class="n">features</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">features</span><span class="p">)</span>
        <span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">v</span><span class="o">.</span><span class="n">label</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">features</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">labels</span><span class="p">),</span> <span class="n">instances_indexes</span></div>


<div class="viewcode-block" id="generate_table_namespace_n_name"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.generate_table_namespace_n_name">[docs]</a><span class="k">def</span> <span class="nf">generate_table_namespace_n_name</span><span class="p">(</span><span class="n">input_file_path</span><span class="p">):</span>
    <span class="n">last</span> <span class="o">=</span> <span class="n">input_file_path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="n">namespace</span> <span class="o">=</span> <span class="n">last</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">table_name</span> <span class="o">=</span> <span class="n">get_timestamp</span><span class="p">()</span>
    <span class="k">return</span> <span class="n">namespace</span><span class="p">,</span> <span class="n">table_name</span></div>


<div class="viewcode-block" id="get_timestamp"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.get_timestamp">[docs]</a><span class="k">def</span> <span class="nf">get_timestamp</span><span class="p">():</span>
    <span class="n">local_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span>
    <span class="n">timestamp</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%Y%m</span><span class="si">%d</span><span class="s2">%H%M%S&quot;</span><span class="p">,</span> <span class="n">local_time</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">timestamp</span></div>


<div class="viewcode-block" id="add_random_mask_for_list_of_values"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.add_random_mask_for_list_of_values">[docs]</a><span class="k">def</span> <span class="nf">add_random_mask_for_list_of_values</span><span class="p">(</span><span class="n">value_list</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">value_list</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">list</span> <span class="ow">or</span> <span class="nb">type</span><span class="p">(</span><span class="n">value_list</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">tuple</span><span class="p">:</span>
        <span class="n">masked_value_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">masks</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">value_list</span><span class="p">:</span>
            <span class="n">masked_value</span><span class="p">,</span> <span class="n">mask</span> <span class="o">=</span> <span class="n">add_random_mask</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">masked_value_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">masked_value</span><span class="p">)</span>
            <span class="n">masks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">mask</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">masked_value_list</span><span class="p">,</span> <span class="n">masks</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;the input is not a list&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="add_random_mask"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.add_random_mask">[docs]</a><span class="k">def</span> <span class="nf">add_random_mask</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
        <span class="n">mask</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random_sample</span><span class="p">(</span><span class="n">value</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">value</span> <span class="o">+</span> <span class="n">mask</span><span class="p">,</span> <span class="n">mask</span>
    <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="s1">&#39;__add__&#39;</span><span class="p">):</span>
        <span class="n">mask</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">value</span> <span class="o">+</span> <span class="n">mask</span><span class="p">,</span> <span class="n">mask</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;does not support type of &quot;</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">value</span><span class="p">))</span></div>


<div class="viewcode-block" id="remove_random_mask_from_list_of_values"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.remove_random_mask_from_list_of_values">[docs]</a><span class="k">def</span> <span class="nf">remove_random_mask_from_list_of_values</span><span class="p">(</span><span class="n">value_list</span><span class="p">,</span> <span class="n">mask_list</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">value_list</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">list</span> <span class="ow">or</span> <span class="nb">type</span><span class="p">(</span><span class="n">value_list</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">tuple</span><span class="p">:</span>
        <span class="n">cleared_value_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">c</span><span class="p">,</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">value_list</span><span class="p">,</span> <span class="n">mask_list</span><span class="p">):</span>
            <span class="n">cleared_value_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">remove_random_mask</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">m</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">cleared_value_list</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;the input is not a list&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="remove_random_mask"><a class="viewcode-back" href="../../../../federatedml.ftl.data_util.html#federatedml.ftl.data_util.common_data_util.remove_random_mask">[docs]</a><span class="k">def</span> <span class="nf">remove_random_mask</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">mask</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">value</span> <span class="o">-</span> <span class="n">mask</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>